﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using GroupCheer.FBGraphAPI.Core;

namespace GroupCheer.FBGraphAPI.API
{
    public class PublishStream
    {
        public static string PostWallMessage(string message, string picture, string link, string name, string caption, string description, string source)
        {
            if (string.IsNullOrEmpty(message))
                throw new ArgumentNullException("Message data cannot be NULL or blank.", "message");

            Dictionary<string, string> httpPostItems = new Dictionary<string, string>();

            httpPostItems.Add("message", message);

            if (!string.IsNullOrEmpty(picture))
                httpPostItems.Add("picture", picture);

            if (!string.IsNullOrEmpty(link))
                httpPostItems.Add("link", link);

            if (!string.IsNullOrEmpty(name))
                httpPostItems.Add("name", name);

            if (!string.IsNullOrEmpty(caption))
                httpPostItems.Add("caption", caption);

            if (!string.IsNullOrEmpty(description))
                httpPostItems.Add("description", description);

            if (!string.IsNullOrEmpty(source))
                httpPostItems.Add("source", source);

            //if (FBGraphAPIConfiguration.EnableAsync)
            //   AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/feed", FBUserContext.UserID), httpPostItems);
            //else
               return Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/feed", FBUserContext.UserID), httpPostItems);
        }

        public static void PostComment(string message)
        {
            Dictionary<string, string> httpPostItems = new Dictionary<string, string>();
            httpPostItems.Add("message", message);
            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/comments", FBUserContext.UserID), httpPostItems);
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/comments", FBUserContext.UserID), httpPostItems);
        }
        public static void Like(string PostID)
        {
            Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/Likes", PostID));
        }

        public static void PostNote(string message, string subject)
        {
            if (string.IsNullOrEmpty(message) || string.IsNullOrEmpty(subject))
                throw new ArgumentNullException("Message or Subject data cannot be NULL or blank.");

            Dictionary<string, string> httpPostItems = new Dictionary<string, string>();

            httpPostItems.Add("subject", subject);
            httpPostItems.Add("message", message);
            
            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/notes", FBUserContext.UserID), httpPostItems);
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/notes", FBUserContext.UserID), httpPostItems);            
        }

        public static void PostLink(string message, string link)
        {
            if (string.IsNullOrEmpty(link))
                throw new ArgumentNullException("link data cannot be NULL or blank.");

            Dictionary<string, string> httpPostItems = new Dictionary<string, string>();

            httpPostItems.Add("link", link);
            httpPostItems.Add("message", message);

            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/links", FBUserContext.UserID), httpPostItems);
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/links", FBUserContext.UserID), httpPostItems);
        }

        public static void PostEvent(string eventname, DateTime startDate, DateTime endDate)
        {
            if (string.IsNullOrEmpty(eventname))
                throw new ArgumentNullException("eventname data cannot be NULL or blank.");

            Dictionary<string, string> httpPostItems = new Dictionary<string, string>();

            httpPostItems.Add("name", eventname);
            httpPostItems.Add("start_time", startDate.ToString("s"));
            httpPostItems.Add("end_time", endDate.ToString("s"));

            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/events", FBUserContext.UserID), httpPostItems);
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/events", FBUserContext.UserID), httpPostItems);
        }

        public static void DoAttendEvent(string eventid)
        {
            if (string.IsNullOrEmpty(eventid))
                throw new ArgumentNullException("eventid data cannot be NULL or blank.");
            
            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/attending", eventid));
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/attending", eventid));            
        }

        public static void DoMaybeAttendEvent(string eventid)
        {
            if (string.IsNullOrEmpty(eventid))
                throw new ArgumentNullException("eventid data cannot be NULL or blank.");

            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/maybe", eventid));
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/maybe", eventid));
        }

        public static void DoDeclineEvent(string eventid)
        {
            if (string.IsNullOrEmpty(eventid))
                throw new ArgumentNullException("eventid data cannot be NULL or blank.");

            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/declined", eventid));
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/declined", eventid));
        }

        public static void CreateAlbum(string name, string message)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name data cannot be NULL or blank.");

            Dictionary<string, string> httpPostItems = new Dictionary<string, string>();

            httpPostItems.Add("name", name);
            httpPostItems.Add("message", message);
            
            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/albums", FBUserContext.UserID), httpPostItems);
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/albums", FBUserContext.UserID), httpPostItems);
        }

        public static void UploadPhoto(string message, string filename, string contenttype, byte[] imageData)
        {
            PostFileData postFileData = new PostFileData()
            {
                Name = "file",
                ContentType = contenttype,
                FileData = imageData,
                FileName = filename
            };

            Dictionary<string, string> httpPostItems = new Dictionary<string, string>();

            httpPostItems.Add("message", message);

            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/photos", FBUserContext.UserID), httpPostItems, postFileData);
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/photos", FBUserContext.UserID), httpPostItems, postFileData);
        }

        public static void UploadPhoto(string albumid, string message, string filename, string contenttype, byte[] imageData)
        {
            if (string.IsNullOrEmpty(albumid))
                throw new ArgumentNullException("albumid data cannot be NULL or blank.");

            PostFileData postFileData = new PostFileData()
            {
                Name = "file",
                ContentType = contenttype,
                FileData = imageData,
                FileName = filename
            };

            Dictionary<string, string> httpPostItems = new Dictionary<string, string>();

            httpPostItems.Add("message", message);

            if (FBGraphAPIConfiguration.EnableAsync)
                AsyncNetwork.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/photos", albumid), httpPostItems, postFileData);
            else
                Network.MakeHttpPost(string.Format("https://graph.facebook.com/{0}/photos", albumid), httpPostItems, postFileData);
        }
    }
}
